package com.ruoyi.merchant.shop.controller;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.merchant.shopverification.domain.ShopVerification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.shop.domain.Shop;
import com.ruoyi.merchant.shop.service.IShopService;
import com.ruoyi.merchant.shop.service.SmsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 店铺信息Controller
 * 
 * @author zzr
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController
{
    @Autowired
    private IShopService shopService;

    @Autowired
    private SmsService smsService;

    /**
     * 查询店铺信息列表
     */
    @RequiresPermissions("merchant:shop:list")
    @GetMapping("/list")
    public TableDataInfo list(Shop shop)
    {
        startPage();
        List<Shop> list = shopService.selectShopList(shop);
        return getDataTable(list);
    }

    /**
     * 全查店铺信息（不分页，默认过滤逻辑删除数据，支持筛选条件）
     * 用途：下拉选择、全量数据加载等场景
     */
    @RequiresPermissions("merchant:shop:query") // 复用现有查询权限，无需新增
    @GetMapping("/getAll") // 接口路径：/shop/getAll
    public AjaxResult getAll(Shop shop) // 支持传递筛选条件（如按名称、地址、审核状态筛选）
    {
        // 调用Service全查方法，返回全量未删除数据
        List<Shop> allList = shopService.selectAllShop(shop);
        return AjaxResult.success(allList); // 直接返回列表（无分页包装）
    }

    /**
     * 导出店铺信息列表
     */
    @RequiresPermissions("merchant:shop:export")
    @Log(title = "店铺信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shop shop)
    {
        List<Shop> list = shopService.selectShopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<Shop>(Shop.class);
        util.exportExcel(response, list, "店铺信息数据");
    }

    /**
     * 获取店铺信息详细信息
     */
    @RequiresPermissions("merchant:shop:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(shopService.selectShopById(id));
    }

    /**
     * 新增店铺信息
     */
    @RequiresPermissions("merchant:shop:add")
    @Log(title = "店铺信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shop shop)
    {
        return toAjax(shopService.insertShop(shop));
    }

    /**
     * 修改店铺信息
     */
    @RequiresPermissions("merchant:shop:edit")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shop shop)
    {
        return toAjax(shopService.updateShop(shop));
    }

    /**
     * 删除店铺信息
     */
    @RequiresPermissions("merchant:shop:remove")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(shopService.deleteShopByIds(ids));
    }

    /**
     * 发送验证码
     */
    @PostMapping("/sendCode")
    public AjaxResult sendCode(@RequestParam String mobile){
        System.out.println("接收到的手机号:"+mobile);
        String result = smsService.sendCode(mobile);
                if ("验证码发送成功".equals(result)) {
            return success(result);
        } else {
            return error(result);
        }
    }

    /**
     * 验证验证码
     */
    @PostMapping("/verifyCode")
    public AjaxResult verifyCode(@RequestBody Map<String, String> params){
        String mobile = params.get("mobile");
        String code = params.get("code");

        if (smsService.verifyCode(mobile, code)) {
            return success("验证码验证成功");
        } else {
            return error("验证码错误或已过期");
        }
    }

    /**
     * 查询店铺信息列表
     */
    @RequiresPermissions("merchant:shop:list")
    @GetMapping("/listAll")
    public TableDataInfo selectAll()
    {
        startPage();
        List<Shop> list = shopService.selectAll();
        return getDataTable(list);
    }

    /** 审核成功调用的接口*/
    @RequiresPermissions("merchant:shop:edit")
    @Log(title = "店铺信息审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/auditStatusTrue")
    public AjaxResult auditStatusTrue(@RequestBody ShopVerification shopVerification)
    {
        return toAjax(shopService.auditStatusTrue(shopVerification));
    }

    /** 审核失败调用的接口*/
    @RequiresPermissions("merchant:shop:edit")
    @Log(title = "店铺信息审核不通过", businessType = BusinessType.UPDATE)
    @PutMapping("/auditStatusFalse")
    public AjaxResult auditStatusFalse(@RequestBody ShopVerification shopVerification)
    {
        return toAjax(shopService.auditStatusFalse(shopVerification));
    }
    /** 用来判断商家是否注册成功*/
    @GetMapping("/shopLogin")
    public AjaxResult shopLogin(@RequestParam("userName") String mobile )
    {
        return success(shopService.selectShopInfo(mobile));
    }
}
